/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.TX.tta;

import Helper.TTAHelper.Helper_DishSale;
import Helper.TTAHelper.Helper_MDSCluster;
import Helper.TTAHelper.Helper_ReservationByAge;
import Helper.TTAHelper.Helper_ReservationByCountry;
import Helper.TTAHelper.Helper_SalesByAge;
import Helper.TTAHelper.Helper_SalesByCountry;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import managedbean.TX.OutletManagement.TrackerManagedOutletMB;
import org.primefaces.component.chart.series.ChartSeries;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.PieChartModel;
import session.TX.TTADisplaySBRemote;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "mdsOutletMB")
@ViewScoped
public class MDSOutletMB {

    @ManagedProperty(value = "#{TrackerOutletAdmin}")
    private TrackerManagedOutletMB portalManagedBean;
    @EJB
    private TTADisplaySBRemote ttaBean;
    private PieChartModel salesByAgeModel;
    private PieChartModel salesByCountryModel;
    private PieChartModel reservationByCountryModel;
    private PieChartModel reservationByAgeModel;
    private PieChartModel dishSaleModel;
    private PieChartModel topClusterModel;
    private CartesianChartModel salesByAgeCartesian;
    private CartesianChartModel salesByCountryCartesian;
    private CartesianChartModel reservationByAgeCartesian;
    private CartesianChartModel reservationByCountryCartesian;
    private CartesianChartModel dishSaleCartesian;
    private CartesianChartModel topClusterCartesian;
    private int salesByAgeChart = 1;
    private int salesByCountryChart = 2;
    private int reservationByAgeChart = 1;
    private int reservationByCountryChart = 2;
    private int topClusterChart = 1;
    private int dishSaleChart = 2;
    private int numCountry_SalesByCountry = 5;
    private int numCountry_ResByCountry = 5;
    private int numCluster_TopCluster = 10;

    /** Creates a new instance of MDSOutletMB */
    public MDSOutletMB() {
    }

    public int getSalesByAgeChart() {
        return salesByAgeChart;
    }

    public void setSalesByAgeChart(int salesByAgeChart) {
        this.salesByAgeChart = salesByAgeChart;
    }

    public int getDishSaleChart() {
        return dishSaleChart;
    }

    public void setDishSaleChart(int dishSaleChart) {
        this.dishSaleChart = dishSaleChart;
    }

    public int getReservationByAgeChart() {
        return reservationByAgeChart;
    }

    public void setReservationByAgeChart(int reservationByAgeChart) {
        this.reservationByAgeChart = reservationByAgeChart;
    }

    public int getReservationByCountryChart() {
        return reservationByCountryChart;
    }

    public void setReservationByCountryChart(int reservationByCountryChart) {
        this.reservationByCountryChart = reservationByCountryChart;
    }

    public int getSalesByCountryChart() {
        return salesByCountryChart;
    }

    public void setSalesByCountryChart(int salesByCountryChart) {
        this.salesByCountryChart = salesByCountryChart;
    }

    public int getTopClusterChart() {
        return topClusterChart;
    }

    public void setTopClusterChart(int topClusterChart) {
        this.topClusterChart = topClusterChart;
    }

    public void chartTypeUpdate() {
    }
    public void salesByCountryCountentUpdate() {
        this.salesByCountryModel=null;
        this.salesByCountryCartesian=null;
    }
    public  void resByCountryContentUpdate(){
        this.reservationByCountryCartesian=null;
        this.reservationByCountryModel=null;
    }
    public void clusterContentUpdate(){
        this.topClusterCartesian = null;
        this.topClusterModel = null;
    }

    private List<Helper_SalesByAge> getSalesByAge() {
        if (portalManagedBean.getOutletId() == null) {
            return null;
        }
        List<Helper_SalesByAge> returnList = ttaBean.getOSalesByAge(portalManagedBean.getOutletId());
        return returnList;
    }

    private List<Helper_SalesByCountry> getSalesByCountry() {
        if (portalManagedBean.getOutletId() == null) {
            return null;
        }
        List<Helper_SalesByCountry> returnList = ttaBean.getOSalesByCountry(portalManagedBean.getOutletId());
        return returnList;
    }

    private List<Helper_ReservationByAge> getReservationByAge() {
        if (portalManagedBean.getOutletId() == null) {
            return null;
        }
        List<Helper_ReservationByAge> returnList =
                ttaBean.getOReservationByAge(portalManagedBean.getOutletId());
        return returnList;
    }

    private List<Helper_ReservationByCountry> getReservationByCountry() {
        if (portalManagedBean.getOutletId() == null) {
            return null;
        }
        List<Helper_ReservationByCountry> returnList =
                ttaBean.getOReservationByCountry(portalManagedBean.getOutletId());
        return returnList;
    }

    private List<Helper_MDSCluster> getAllClusters() {
        if (portalManagedBean.getOutletId() == null) {
            return null;
        }
        List<Helper_MDSCluster> returnList =
                ttaBean.getOAllClusters(portalManagedBean.getOutletId());
        return returnList;
    }

    private List<Helper_DishSale> getDishSale() {
        if (portalManagedBean.getOutletId() == null) {
            return null;
        }
        List<Helper_DishSale> returnList =
                ttaBean.getODishSale(portalManagedBean.getOutletId());
        return returnList;
    }

    private void createSalesByAgeModel() {
        if (salesByAgeModel == null) {
            salesByAgeModel = new PieChartModel();
            List<Helper_SalesByAge> allRecords = this.getSalesByAge();
            for (Helper_SalesByAge aRecord : allRecords) {
                System.out.println("Record:" + aRecord.getGroup() + "," + aRecord.getSales());
                salesByAgeModel.set(aRecord.getGroup(), aRecord.getSales());
            }
        }
    }

    private void createSalesByCountryModel() {
        if (salesByCountryModel == null) {
            salesByCountryModel = new PieChartModel();
            List<Helper_SalesByCountry> allRecords = this.getSalesByCountry();
            Double otherSale = new Double(0);
            for (int i = 0; i < allRecords.size(); i++) {
                Helper_SalesByCountry aRecord = allRecords.get(i);
                //only display countries which acounts for more than 2% of total sales
                if (i < numCountry_SalesByCountry) {
                    salesByCountryModel.set(aRecord.getGroup(), aRecord.getSales());
                } else {
                    otherSale += aRecord.getSales();
                }
            }
            if (otherSale > 0) {
                salesByCountryModel.set("Others", otherSale);
            }
        }
    }

    private void createReservationByAgeModel() {
        if (reservationByAgeModel == null) {
            reservationByAgeModel = new PieChartModel();

            List<Helper_ReservationByAge> allRecords = this.getReservationByAge();
            for (Helper_ReservationByAge aRecord : allRecords) {
                reservationByAgeModel.set(aRecord.getGroup(), aRecord.getSales());
            }
        }
    }

    private void createReservationByCountryModel() {
        if (reservationByCountryModel == null) {
            reservationByCountryModel = new PieChartModel();
            List<Helper_ReservationByCountry> allRecords = this.getReservationByCountry();
            Long otherRes = new Long(0);
            for (int i = 0; i < allRecords.size(); i++) {
                Helper_ReservationByCountry aRecord = allRecords.get(i);
                //only display countries which acounts for more than 2% of total sales
                if (i < numCountry_ResByCountry) {
                    reservationByCountryModel.set(aRecord.getGroup(), aRecord.getSales());
                } else {
                    otherRes += aRecord.getSales();
                }
            }
            reservationByCountryModel.set("Others", otherRes);
        }
    }

    private void createDishSaleModel() {
        if (dishSaleModel == null) {
            dishSaleModel = new PieChartModel();

            List<Helper_DishSale> allRecords = this.getDishSale();
            Long rest = new Long(0);
            for (int i = 0; i < allRecords.size(); i++) {
                if (i < 10) {
                    dishSaleModel.set(allRecords.get(i).getDishName(), allRecords.get(i).getSale());
                } else {
                    rest += allRecords.get(i).getSale();
                }
            }
            dishSaleModel.set("Others", rest);
        }
    }

    private void createTopClusterModel() {
        if (topClusterModel == null) {
            topClusterModel = new PieChartModel();

            List<Helper_MDSCluster> allRecords = this.getAllClusters();
            Long otherSale = new Long(0);
            for (int i = 0; i < allRecords.size(); i++) {
                Helper_MDSCluster aRecord = allRecords.get(i);
                //only display countries which acounts for more than 2% of total sales
                if (i < numCluster_TopCluster) {
                    topClusterModel.set(aRecord.getDescription(), aRecord.getNumber());
                } else {
                    otherSale += aRecord.getNumber();
                }
            }
            if (otherSale > 0) {
                topClusterModel.set("Others", otherSale);
            }
        }
    }

    private void createDishSaleCartesian() {
        if (dishSaleCartesian == null) {
            dishSaleCartesian = new CartesianChartModel();
            ChartSeries sales = new ChartSeries();
            sales.setLabel("Dish Sales");
            List<Helper_DishSale> allRecords = this.getDishSale();
            for (Helper_DishSale aRecord : allRecords) {
                sales.set(aRecord.getDishName(), aRecord.getSale());
            }
            dishSaleCartesian.addSeries(sales);
        }
    }

    private void createSalesByAgeCartesian() {
        if (salesByAgeCartesian == null) {
            salesByAgeCartesian = new CartesianChartModel();
            ChartSeries sales = new ChartSeries();
            sales.setLabel("sales");
            List<Helper_SalesByAge> allRecords = this.getSalesByAge();
            for (Helper_SalesByAge aRecord : allRecords) {
                sales.set(aRecord.getGroup(), aRecord.getSales());
            }
            salesByAgeCartesian.addSeries(sales);
        }
    }

    private void createSalesByCountryCartesian() {
        if (salesByCountryCartesian == null) {
            salesByCountryCartesian = new CartesianChartModel();
            ChartSeries sales = new ChartSeries();
            sales.setLabel("sales");
            List<Helper_SalesByCountry> allRecords = this.getSalesByCountry();
            Double otherSale = new Double(0);
            for (int i = 0; i < allRecords.size(); i++) {
                Helper_SalesByCountry aRecord = allRecords.get(i);
                //only display countries which acounts for more than 2% of total sales
                if (i < numCountry_SalesByCountry) {
                    sales.set(aRecord.getGroup(), aRecord.getSales());
                } else {
                    otherSale += aRecord.getSales();
                }
            }
            sales.set("Others", otherSale);
            salesByCountryCartesian.addSeries(sales);
        }
    }

    private void createReservationByAgeCartesian() {
        if (reservationByAgeCartesian == null) {
            reservationByAgeCartesian = new CartesianChartModel();
            ChartSeries sales = new ChartSeries();
            sales.setLabel("reservations");
            List<Helper_ReservationByAge> allRecords = this.getReservationByAge();
            for (Helper_ReservationByAge aRecord : allRecords) {
                sales.set(aRecord.getGroup(), aRecord.getSales());
            }
            reservationByAgeCartesian.addSeries(sales);
        }
    }

    private void createReservationByCountryCartesian() {
        if (reservationByCountryCartesian == null) {
            reservationByCountryCartesian = new CartesianChartModel();
            ChartSeries sales = new ChartSeries();
            sales.setLabel("reservations");
            List<Helper_ReservationByCountry> allRecords = this.getReservationByCountry();
            Long otherRes = new Long(0);
            for (int i = 0; i < allRecords.size(); i++) {
                Helper_ReservationByCountry aRecord = allRecords.get(i);
                //only display countries which acounts for more than 2% of total sales
                if (i < numCountry_ResByCountry) {
                    sales.set(aRecord.getGroup(), aRecord.getSales());
                } else {
                    otherRes += aRecord.getSales();
                }
            }
            sales.set("Others", otherRes);
            reservationByCountryCartesian.addSeries(sales);
        }
    }

    private void createTopClusterCartesian() {
        if (topClusterCartesian == null) {
            topClusterCartesian = new CartesianChartModel();
            ChartSeries clusters = new ChartSeries();
            clusters.setLabel("cluster");
            List<Helper_MDSCluster> allRecords = this.getAllClusters();
            Long other = new Long(0);
            for (int i = 0; i < allRecords.size(); i++) {
                Helper_MDSCluster aRecord = allRecords.get(i);
                //only display countries which acounts for more than 2% of total sales
                if (i < numCluster_TopCluster) {
                    clusters.set(aRecord.getDescription(), aRecord.getNumber());
                } else {
                    other += aRecord.getNumber();
                }
            }
            clusters.set("others", other);
            topClusterCartesian.addSeries(clusters);
        }
    }

    public TrackerManagedOutletMB getPortalManagedBean() {
        return portalManagedBean;
    }

    public void setPortalManagedBean(TrackerManagedOutletMB portalManagedBean) {
        this.portalManagedBean = portalManagedBean;
    }

    public PieChartModel getSalesByAgeModel() {
        createSalesByAgeModel();
        return salesByAgeModel;
    }

    public CartesianChartModel getSalesByAgeCartesian() {
        createSalesByAgeCartesian();
        return salesByAgeCartesian;
    }

    public PieChartModel getSalesByCountryModel() {
        createSalesByCountryModel();
        return salesByCountryModel;
    }

    public CartesianChartModel getSalesByCountryCartesian() {
        createSalesByCountryCartesian();
        return salesByCountryCartesian;
    }

    public PieChartModel getReservationByAgeModel() {
        createReservationByAgeModel();
        return reservationByAgeModel;
    }

    public CartesianChartModel getReservationByAgeCartesian() {
        createReservationByAgeCartesian();
        return reservationByAgeCartesian;
    }

    public PieChartModel getReservationByCountryModel() {
        createReservationByCountryModel();
        return reservationByCountryModel;
    }

    public CartesianChartModel getReservationByCountryCartesian() {
        createReservationByCountryCartesian();
        return reservationByCountryCartesian;
    }

    public PieChartModel getTopClusterModel() {
        createTopClusterModel();
        return topClusterModel;
    }

    public CartesianChartModel getTopClusterCartesian() {
        createTopClusterCartesian();
        return topClusterCartesian;
    }

    public PieChartModel getDishSaleModel() {
        createDishSaleModel();
        return dishSaleModel;
    }

    public CartesianChartModel getDishSaleCartesian() {
        createDishSaleCartesian();
        return dishSaleCartesian;
    }

    public void setNumCluster_TopCluster(int numCluster_TopCluster) {
        this.numCluster_TopCluster = numCluster_TopCluster;
    }

    public void setNumCountry_ResByCountry(int numCountry_ResByCountry) {
        this.numCountry_ResByCountry = numCountry_ResByCountry;
    }

    public void setNumCountry_SalesByCountry(int numCountry_SalesByCountry) {
        this.numCountry_SalesByCountry = numCountry_SalesByCountry;
    }

    public int getNumCluster_TopCluster() {
        return numCluster_TopCluster;
    }

    public int getNumCountry_ResByCountry() {
        return numCountry_ResByCountry;
    }

    public int getNumCountry_SalesByCountry() {
        return numCountry_SalesByCountry;
    }
    
}
